<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>个人页面</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css">
    <link rel="stylesheet" href="/css/userProfiles.css">
    <link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
    <script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {
            var id = getParams().id;
            //各选项卡的分页参数
            //选项卡分页数据
            var tagData = [];
            //当前的选项卡
            var currentTag;

            //拿到当前登录的用户，要求必须登录之后才能评论
            var currentuser ;
            currentuser = JSON.parse(sessionStorage.getItem("user"));

            //给私信按钮绑定点击事件
            $("#letterBtn").click(function () {
                //判断当前有没有登录，没有登录的话就不让发私信
                if(currentuser){
                    window.location.href = "/mine/sms.html?id="+id;//将被私信的用户也带过去
                }else{
                    alert("想要私信就登录先啦，匿名的好友吗？")
                }
            })


            $("#pills-tab a").click(function () {
                var index = $(this).data(index).index;
                currentTag = tagData[index];
                console.log(currentTag);
            })



            //回显个人信息
            $.get('/users/' + id, function (data) {
                console.log(data);
                $(".bg").renderValues(data,{
                    //粉丝按钮自定义href函数
                    watchHandle:function (ele,value) {
                        $(ele).attr("href","/fansOrWatch.html?type=watch&id="+id);
                    },
                    //关注按钮自定义href函数
                    fansHandle:function (ele,value) {
                        $(ele).attr("href","/fansOrWatch.html?type=fans&id="+id);
                    }
                });
                $(".bg").css("background-image", "url('" + data.coverImgUrl + "')");
            })
            //回显用户的所有游记
            $.get("/users/" + id + "/travels",{state:2,pageSize:2}, function (data) {
                tagData[1] = {id:1,currentPage:1,pages:data.pages,data:data};
                console.log(data.size);
                //回显选项卡数量内容
                $("#travels-tab span").html(data.total);
                travelRenderView(data);
                //页面数据初始化
                currentTag=tagData[1];
            })


            //用户关注按钮实现
            $("#followBtn").click(function () {
                $.post("/users/" + id + "/concern", function (data) {
                    if (data.success) {
                        console.log(data);
                        //刷新页面更新数据
                        location.reload()
                    } else {
                        $(document).dialog({
                            titleShow: false,
                            content: data.msg,
                            autoClose: 1500
                        });
                    }
                });

            })

            //查询用户是否已经被登录的用户关注
            $.get("/users/" + id + "/isConcern", function (data) {
                if (!data) {
                    $("#followBtn").attr("disabled", "disabled").html("已关注");
                }
            })

            //查询用户所有点评
            $.get("/users/"+id+"/strategyComments",{pageSize:2},function (data) {
                //回显点评的数量
                $("#comments-tab span").html(data.total);
                tagData[2] = {id:2,currentPage:1,pages:data.pages,data:data};

                commentRenderView(data);
            })
            //滚动事件
            $(window).scroll(function () {
                if(isEnd()){
                    if(currentTag.currentPage<currentTag.pages){
                        //如果是当前界面是游记
                        if(currentTag.id==1){
                            $.get("/users/" + id + "/travels",{state:2,pageSize:2,currentPage:++(currentTag.currentPage)}, function (data) {
                                //合并数组到原来数据
                                $.merge(currentTag.data.list,data.list);
                                travelRenderView(currentTag.data)
                            })

                        }else if (currentTag.id=2){//当前界面是点评
                            $.get("/users/"+id+"/strategyComments",{pageSize:2,currentPage:++(currentTag.currentPage)},function (data) {
                                //回显点评的数量
                                console.log(currentTag.data.list);
                                console.log(data.list);
                                //合并数组
                                $.merge(currentTag.data.list,data.list);
                                //渲染数据
                                commentRenderView(currentTag.data)
                            })
                        }

                    }else{
                        //如果滚动到了底部就给个友好的提示，使用dialog这个插件进行有好的提示
                        $(document).dialog({
                            type : 'notice',
                            infoText: "啊,轻点",
                            autoClose: 1500,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                }
            })
        })

        function isEnd() {
            return parseInt($(window).height())+$(window).scrollTop()+1>=$(document).height();
        }



        //游记渲染方法
        function travelRenderView(data){
            $("#travels").renderValues(data, {
                handle: function (ele, value) {
                    ele.href = "/travelContent.html?id=" + value;
                }

            });
        }
        //点评渲染方法
        function commentRenderView(data){
            console.log(data);
            //回显星级
            $("#comments").renderValues(data,{
                handle:function (ele,value) {
                    var num = 0;
                    while (num<value){
                        $(ele).append('<i class="fa fa-star"></i>');
                        num++;
                    }
                    while(num<5){
                        $(ele).append('<i class="fa fa-star-o"></i>');
                        num++;
                    }
                },
                toStrategy:function (ele,value) {
                    ele.href="strategyCatalogs.html?strategyId="+value;
                },
                commentHandler:function (ele,value) {
                    ele.href="/strategyComment.html?id="+value;
                }
            });
        }

    </script>
</head>
<body>
<div class="container bg" style="background-image: url(&quot;/img/user/bg.jpeg&quot;);">
    <a href="javascript:window.history.go(-1)" class="my-arrow">
        <span><i class="fa fa-chevron-left fa-2x"></i></span>
    </a>
    <div class="container">
        <div class="info">
            <img class="rounded-circle" width="17%" render-src="headImgUrl">
            <p class="name" render-html="nickName"></p>
            <p render-html="sign"></p>

            <div class="row num">
                <div class="col-2 border border-left-0 border-top-0 border-bottom-0">
                    <div>
                        <a class="watchBtn" render-key="id" render-fun="watchHandle">
                            <span render-html="watchNum"></span>
                        </a>
                    </div>
                    <div>
                        <a class="watchBtn" render-key="id" render-fun="watchHandle">
                            <span>关注</span>
                        </a>
                    </div>
                </div>
                <div class="col-2">
                    <div>
                        <a class="fansBtn" render-key="id" render-fun="fansHandle">
                            <span render-html="fansNum"></span>
                        </a>
                    </div>
                    <div>
                        <a class="fansBtn" render-key="id" render-fun="fansHandle">
                            <span>粉丝</span>
                        </a>
                    </div>
                </div>
                <div class="col">
                    <button class="btn btn-outline-light ibtn" id="letterBtn">私信</button>




                    <button class="btn btn-success ibtn" id="followBtn">关注</button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="tabs">
    <ul class="nav nav-pills nav-justified  border border-left-0 border-top-0 border-right-0" id="pills-tab"
        role="tablist">
        <li class="nav-item">
            <a class="nav-link active" id="travels-tab" data-toggle="pill" data-index="1" href="#travels">
                TA的游记[<span>1</span>]
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link " id="comments-tab" data-toggle="pill" data-index="2" href="#comments">
                TA的点评[<span></span>]
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" id="pills-contact-tab" data-toggle="pill" data-index="3" href="#pills-contact">
                TA的旅行单[<span>0</span>]
            </a>
        </li>
    </ul>
    <div class="container tab-content" id="pills-tabContent">
        <div class="tab-pane fade show active" id="travels">

            <dl class="list" render-loop="list">
                <dd>
                    <a render-key="list.id" render-fun="handle">
                        <img render-src="list.coverUrl">
                        <p><span class="title" render-html="list.title"></span>
                            <span class="span-right">
						 				<span>0</span> <i class="fa fa-thumbs-o-up"></i></span>
                        </p>
                    </a>
                    <hr>
                </dd>
            </dl>
        </div>

        <div class="comment tab-pane fade " id="comments">
            <input type="hidden" name="currentPage" value="1">
            <input type="hidden" name="pageSize" value="5">
            <input type="hidden" name="rows" value="1">
            <div render-loop="list">
                <div class="row">
                    <div class="col-2 comment-head">
                        <img class="rounded-circle" render-src="list.author.headImgUrl">
                    </div>
                    <div class="col">
                        <span class="comment-star" render-key="list.grade" render-fun="handle"></span>

                        <span class="comment-date" render-html="list.createDate"></span>
                        <div class="comment-content">
                            <a render-key="list.id" render-fun="commentHandler" data-id="4">
                                <p render-html="list.content"></p>
                            </a>
                        </div>
                        <ul class="comment-img" render-loop="list.images">
                            <li><img render-src="list.images.imageUrl"></li>
                        </ul>

                        <div class="comment-link">
                            <a  render-key="list.strategy.id" render-fun="toStrategy" data-id="2">
                                <img render-src="list.strategy.coverUrl"> <span render-html="list.strategy.title"></span>
                                <i class="fa fa-angle-right fa-2x"></i>
                            </a>
                        </div>

                    </div>
                </div>
            </div>

        </div>
        <div class="tab-pane fade" id="pills-contact">.3333..</div>
    </div>
</div>


</body>
</html>